//
//  MyWidgetContentView.swift
//  TimMusic
//
//  Created by nkl on 2024/10/24.
//

import Foundation
import SnapKit
import SwiftUI

struct MyWidgetContentView: View {
    @ObservedObject var viewModel: MyWidgetContentViewModel

    var didSelectedItem: (WidgetItemModel) -> Void = { _ in }
    let smallSize = TimSize.scaleSmallSize(colum: 3)
    let mediumSize = TimSize.mediumSize

    var body: some View {
        ScrollView(showsIndicators: false) {
            VStack(alignment: .leading) {
                if !viewModel.smallDatas.isEmpty {
                    Text("Small Widget").foregroundColor(.white)
                    LazyVGrid(columns: [.init(), .init(), .init()]) {
                        ForEach(viewModel.smallDatas, id: \.uidString) { data in
                            VStack {
                                ZStack {
                                    AnyView(TimClassMap.map(name: data.itemModel.fullName).frame(width: TimSize.smallSize.width, height: TimSize.smallSize.height)).cornerRadius(22).scaleEffect(smallSize.width / TimSize.smallSize.width)
                                }.frame(height: smallSize.height)
                                if viewModel.isEditing {
                                    ZStack {
                                        Color.white
                                        Text("Delete").font(.system(size: 14)).foregroundColor(.black)
                                    }.frame(width: 60, height: 22)
                                        .cornerRadius(11)
                                        .onTapGesture {
                                            viewModel.deleteMyWidgets(id: data.uidString)
                                        }
                                }
                            }
                        }
                    }.padding(.top)
                }

                if !viewModel.mediaDatas.isEmpty {
                    if !viewModel.smallDatas.isEmpty {
                        Spacer().frame(height: 72)
                    }
                    Text("Medium Widget").foregroundColor(.white)
                    LazyVGrid(columns: [.init(), .init()]) {
                        ForEach(viewModel.mediaDatas, id: \.uidString) { data in
                            VStack {
                                ZStack {
                                    AnyView(TimClassMap.map(name: data.itemModel.fullName).frame(width: mediumSize.width, height: mediumSize.height)).cornerRadius(22).scaleEffect(TimSize.scaleSpecialMediumSize.width / TimSize.mediumSize.width)
                                }.frame(height: TimSize.scaleSpecialMediumSize.height)
                                if viewModel.isEditing {
                                    ZStack {
                                        Color.white
                                        Text("Delete").font(.system(size: 14)).foregroundColor(.black)
                                    }.frame(width: 60, height: 22)
                                        .cornerRadius(11)
                                        .onTapGesture {
                                            viewModel.deleteMyWidgets(id: data.uidString)
                                        }
                                }
                            }
                        }
                    }
                }
            }
        }
        .padding(.horizontal, 16)
        .padding(.top, 20)
        .onAppear {
            viewModel.requestData()
        }
    }
}
